package com.funmi.lizip.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.funmi.lizip.common.base.R;
import com.funmi.lizip.entity.Device;
import com.funmi.lizip.entity.dto.device.DeviceQueryRequest;
import com.funmi.lizip.mapper.DeviceMapper;
import com.funmi.lizip.service.DeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zzy
 * @since 2022-11-02
 */
@Service
@Transactional
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements DeviceService {


    @Autowired
    private DeviceMapper deviceMapper;

//    /**
//     * 获取列表
//     * @return
//     */
//    @Override
//    public List<Device> getAll() {
//        return deviceMapper.getAll();
//
//    }




    /**
     * 拿到Device_sn
     * @param
     * @return
     */
    @Override
    public Integer getSn(String imei) {
        return deviceMapper.getSn(imei);
    }



//    @Override
//    public List<String> getType(Integer deviceId) {
//      return   deviceMapper.getType(deviceId);
//
//    }



    /**
     * 设置设备下线
     *
     * @param stringObjectHashMap
     * @return
     */
    @Override
    public R robeInstrumentMessage(HashMap<String, Object> stringObjectHashMap) {
        String s = JSONObject.toJSONString(stringObjectHashMap);
        // 截断
        HashMap<String, Object> hashMap = JSON.parseObject(s, HashMap.class);
        Object payload = hashMap.get("payload");
        String s2 = payload.toString();

        HashMap<String, Object> hashMap2 = JSON.parseObject(s2, HashMap.class);
        Object ext = hashMap2.get("ext");
        // 取出etc 之后的内容
        String s1 = ext.toString();

        // 截取出imei
        HashMap hashMap1 = JSONObject.parseObject(s1, HashMap.class);
        Object objectImei = hashMap1.get("imei");
        Integer deviceId = this.getSn(objectImei.toString());
        Device device = this.getById(deviceId);
        //Device device = this.findDeviceByImei(objectImei.toString());

        if (device.getOnline()) {
            device.setOnline(false);
            this.updateById(device);
            return R.success().message("设备成功下线");
        } else {
            return R.fail().message("设备已下线");
        }

    }

//    @Override
//    public void downLoad(HttpServletResponse response, DeviceQueryRequest request) {
//        // 设置响应内容
//        response.setContentType("application/vnd.ms-excel");
//        response.setCharacterEncoding("UTF-8");// 防止下载的文件名字乱码
//        try {
//        // 文件以附件形式下载
//            response.setHeader("Content-disposition",
//                    "attachment;filename=设备清单" + ".xlsx");
//        // System.out.print(empQuery.toString());
//            EasyExcel.write(response.getOutputStream(), Device.class).sheet("文件下载")
//                    .doWrite(deviceMapper.getAll(request));
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }

        @Override
    public List<Device> getAll(DeviceQueryRequest device) {
        return deviceMapper.getAll(device);
    }


    /***
     * 根据设备编号查询设备类型
     * @param imei
     * @return
     */
    @Override
    public String getTypeByDeviceSn(String imei) {
        return deviceMapper.getType(imei);

    }
}
